package com.lzj.server.common.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzj.server.common.model.Log;
import com.lzj.server.common.service.LogService;
import com.lzj.server.constant.enums.LogTypeEnum;
import com.lzj.server.repository.mapper.LogMapper;
import com.lzj.server.service.UserService;
import com.lzj.server.utils.LogUtils;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.redisson.api.RBlockingQueue;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Resource
    private LogMapper logMapper;
    ;

    @PostConstruct
    public void startConsumer() {
        Thread thread = new Thread(() -> {
            RBlockingQueue<Log> queue = LogUtils.getLogQueue();
            while (true) {
                try {
                    Log log = queue.take(); // 阻塞等待
                    logMapper.insert(log);
                } catch (Exception e) {
                    log.error("日志处理失败", e);
                }
            }
        }, "Log-Consumer-Thread");
        thread.setDaemon(true);
        thread.start();
    }

}
